package org.phalanx.client.mapeditor.editortools;

import java.util.ArrayList;

import org.phalanx.map.MapCoordinates;

public class MapSelectionMatrix {
	MapCoordinates startSelection;
	MapCoordinates endSelection;
	
	public MapSelectionMatrix(){}
	
	public MapSelectionMatrix(MapCoordinates start, MapCoordinates end){
		this.setSelectionMatrixPoints(start, end);
	}
	
	public void setSelectionMatrixPoints(MapCoordinates start, MapCoordinates end){
		setSelectionMatrix(start,end);
	}
	
	public MapCoordinates getSelectionMatrixStart(){
		return this.startSelection;
	}
	
	public MapCoordinates getSelectionMatrixEnd(){
		return this.endSelection;
	}
	
	public void setSelectionMatrix(MapCoordinates start, MapCoordinates end){
		if (start.getX() < 0){
			start.setX(0);
		}
		if (start.getY() < 0){
			start.setY(0);
		}
		
		if (end.getX() < 0){
			end.setX(0);
		}
		
		if (end.getY() < 0){
			end.setY(0);
		}
		
		if (start.getX() > end.getX()){
			int tempVar = start.getX().intValue();
			start.setX(end.getX().intValue());
			end.setX(tempVar);
		}
		
		if (start.getY() > end.getY()){
			int tempVar = start.getY().intValue();
			start.setY(end.getY().intValue());
			end.setY(tempVar);
		}
		
		this.startSelection = start;
		this.endSelection = end;
	}
	
	public ArrayList<MapCoordinates> getSelectionMatrixArray(){
		ArrayList<MapCoordinates> matrixArray = new ArrayList<MapCoordinates>();
		
		if (getSelectionMatrixStart() != null && getSelectionMatrixEnd() != null){
			Integer pxStart = getSelectionMatrixStart().getX();
			Integer pyStart = getSelectionMatrixStart().getY();
			Integer pxEnd = getSelectionMatrixEnd().getX();
			Integer pyEnd = getSelectionMatrixEnd().getY();
			
			for (int i = pxStart; i <= pxEnd; i++){
				for (int j = pyStart; j <= pyEnd ; j++){
					matrixArray.add(new MapCoordinates(i,j));
				}
			}
		}
		
		return matrixArray;
	}
}
